package thread;

public class MultiSelling {
	public static void main(String[] args) {
		Ticket[] tickets = new Ticket[100];
		for(int i=0; i<tickets.length; i++){
			tickets[i] = new Ticket(i, false);
		}
		Seller s1 = new Seller(tickets, "john");
		Seller s2 = new Seller(tickets, "tom");
		s1.start();
		s2.start();
	}
}

class Seller extends Thread{
	private Ticket[] ts;
	private String name;
	public Seller(Ticket[] ts,String name){
		super();
		this.ts = ts;
		this.name = name;
	}
	
	@Override
	public void run() {
		for(int i=0; i<ts.length; i++){
			synchronized (ts) {
				if(!ts[i].isSold()){
					try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					ts[i].setSold(true);
					System.out.println(name+" is selling "+ts[i].getNum());
				}
			}
		}
	}
}

class Ticket{
	private int num;
	private boolean sold;
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public boolean isSold() {
		return sold;
	}
	public void setSold(boolean sold) {
		this.sold = sold;
	}
	public Ticket(int num, boolean sold) {
		super();
		this.num = num;
		this.sold = sold;
	}
	
}
